//
//  DHS2_ProductInfoController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/21.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_ProductInfoController.h"
#import "DHS2_ProductInfo_DisCell.h"
#import "DHS2_ProductInfo_WebCell.h"
#import "DHS2_ProductInfo_BottomCell.h"

@interface DHS2_ProductInfoController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSDictionary *product;
@property (strong, nonatomic) NSDictionary *productDetailDic;

@property (strong, nonatomic) NSString *numberStr;
@property (strong, nonatomic) NSNumber *webHeight;

@end

@implementation DHS2_ProductInfoController

- (instancetype)initWithProduct:(NSDictionary *)product
{
    self = [super init];
    if (self) {
        self.product = product;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.numberStr = @"";
    
    self.titleLabel.text = @"商品信息";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_ProductInfo_DisCell" bundle:nil] forCellReuseIdentifier:kDHS2_ProductInfo_DisCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_ProductInfo_WebCell" bundle:nil] forCellReuseIdentifier:kDHS2_ProductInfo_WebCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_ProductInfo_BottomCell" bundle:nil] forCellReuseIdentifier:kDHS2_ProductInfo_BottomCell];
    
    [self loadDetailData];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"product_info_webViewDidFinishLoad"]) {
        self.webHeight = [userInfo objectForKey:@"height"];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }
}

- (void)loadDetailData{
    [[DHNetAPIClient shareDHNetAPIClient] request_products_detailWithDictionary:@{@"product_id":[self.product objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
        if (data) {
            self.productDetailDic = [data objectForKey:@"detail"];
            [self.tableView reloadData];
        }
    }];
    
    
    [[DHNetAPIClient shareDHNetAPIClient] request_brands_products_countWithDictionary:@{@"brand_id":[[self.product objectForKey:@"brand"] objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
        self.numberStr = [NSString stringWithFormat:@"%@件",[data objectForKey:@"count"]];
        [self.tableView reloadRow:2 inSection:0 withRowAnimation:UITableViewRowAnimationNone];;
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSArray *properties = [self.productDetailDic objectForKey:@"properties"];
        CGFloat height = 35;
        if (properties.count >= 2) {
            NSDictionary *dic = [properties objectAtIndex:1];
            NSString *vaule = [dic objectForKey:@"value"];
            CGFloat valueHeight = [vaule heightForFont:[UIFont systemFontOfSize:15] width:kScreenWidth - 84];
            height += valueHeight;
        }
        return height + 55;
    }else if (indexPath.row == 1){
        if (self.webHeight) {
            return [self.webHeight floatValue] + 55;
        }else{
            return 70;
        }
    }else{
        return 250;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHS2_ProductInfo_DisCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_ProductInfo_DisCell forIndexPath:indexPath];
        [cell updateCell:self.productDetailDic];
        return cell;
    }else if (indexPath.row == 1){
        DHS2_ProductInfo_WebCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_ProductInfo_WebCell forIndexPath:indexPath];
        [cell updateCell:self.productDetailDic];
        return cell;
    }else{
        DHS2_ProductInfo_BottomCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_ProductInfo_BottomCell forIndexPath:indexPath];
        [cell updateCell:self.productDetailDic];
        cell.numberLabel.text = self.numberStr;
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
